package com.opc.Service.impl;

import com.alibaba.fastjson.JSON;
import com.fazecast.jSerialComm.SerialPort;
import com.opc.Service.JSerialCommIOService;
import com.opc.properties.JSerialCommIOProperties;
import com.opc.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

//("jSerialCommIOServiceImpl")
@Service
@Slf4j
public class jSerialCommIOServiceImpl implements JSerialCommIOService {

    @Autowired
    private JSerialCommIOProperties jSerialCommIOProperties;
    @Autowired
    private WebSocketServer webSocketServer;

    @Override
    public boolean openPort(SerialPort serialPort) {
//            //System.out.println("baudRate是"+jSerialCommIOProperties.getBaudRate());
            serialPort.setBaudRate(jSerialCommIOProperties.getBaudRate());
//            //System.out.println("dataBits是"+jSerialCommIOProperties.getDataBits());
            serialPort.setNumDataBits(jSerialCommIOProperties.getDataBits());
//            //System.out.println("stopBits是"+jSerialCommIOProperties.getStopBits());
            serialPort.setNumStopBits(jSerialCommIOProperties.getStopBits());
//            //System.out.println("parity是"+jSerialCommIOProperties.getParity());
            serialPort.setParity(jSerialCommIOProperties.getParity());
            return serialPort.openPort();
    }

    @Override
    public void closePort(SerialPort serialPort) {
       if (serialPort != null && serialPort.isOpen()) {
           serialPort.closePort();
       }
    }

    @Override
    @Async
    public void buzzerControl() {
        SerialPort serialPort = SerialPort.getCommPort(jSerialCommIOProperties.getPortName());
        //System.out.println("连接蜂鸣器响串口："+jSerialCommIOProperties.getPortName());
        log.info("连接蜂鸣器响串口："+jSerialCommIOProperties.getPortName());
        boolean x = openPort(serialPort);
        //System.out.println("蜂鸣器响串口状态："+x);
        log.info("串口状态："+x);

        if (openPort(serialPort)) {
            //System.out.println("蜂鸣器响串口连接成功！");
            log.info("蜂鸣器响串口连接成功！");
            try {
                //黄灯灭00 5A 53 00 06 04 00 00 B7
//                byte[] buffer1 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0xB7
//                };
                //绿灯灭00 5A 53 00 06 08 00 00 BB
                byte[] buffer2 = new byte[] {
                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0xBB
                };
                //红灯亮00 5A 53 00 05 02 00 00 B4
                byte[] buffer3 = new byte[] {
                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00,
                        (byte) 0x05, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0xB4
                };
                // 蜂鸣器响五秒自动断开00 5A 53 00 14 01 00 05 C7信号
                byte[] buffer = new byte[] {
                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00,
                        (byte) 0x14, (byte) 0x01, (byte) 0x00, (byte) 0x05, (byte) 0xC7
                };
//                serialPort.writeBytes(buffer1, buffer.length);
                serialPort.writeBytes(buffer2, buffer.length);
                serialPort.writeBytes(buffer3, buffer.length);
                serialPort.writeBytes(buffer, buffer.length);
                
                // 读取回复数据
//                byte[] readBuffer = new byte[1024];
//                int bytesRead1 = serialPort.readBytes(readBuffer, readBuffer.length);
//                if (bytesRead1 > 0) {
//                    // 处理回复数据
//                    //System.out.println("蜂鸣器回复数据：" +
//                            new String(readBuffer, 0, bytesRead1));
//                }
            } finally {
                closePort(serialPort);
            }
        } else {
            Map map = new HashMap();
            map.put("message", "..串口连接失败，请检查串口("+
                    jSerialCommIOProperties.getPortName()+")..");
            String json = JSON.toJSONString(map);
            webSocketServer.sendToAllClient(json);
            log.info("蜂鸣器响串口连接失败！");
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }

    @Override
    //@Async
    public void redUp() {
        SerialPort serialPort = SerialPort.getCommPort(jSerialCommIOProperties.getPortName());
        //System.out.println("连接串口："+jSerialCommIOProperties.getPortName());
        boolean x = openPort(serialPort);
        //System.out.println("串口状态："+x);
        if (openPort(serialPort)) {
            //System.out.println("红灯亮串口连接成功！");
            log.info("红灯亮串口连接成功！");
            try {
//                //黄灯灭00 5A 53 00 06 04 00 00 B7
//                byte[] buffer1 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0xB7
//                };
                //绿灯灭00 5A 53 00 06 08 00 00 BB
                byte[] buffer2 = new byte[] {
                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0xBB
                };
                //红灯亮00 5A 53 00 05 02 00 00 B4
                byte[] buffer = new byte[] {
                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00,
                        (byte) 0x05, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0xB4
                };
//                serialPort.writeBytes(buffer1, buffer.length);
                serialPort.writeBytes(buffer2, buffer.length);
                serialPort.writeBytes(buffer, buffer.length);
                
                // 读取回复数据
//                byte[] readBuffer = new byte[1024];
//                int bytesRead1 = serialPort.readBytes(readBuffer, readBuffer.length);
//                if (bytesRead1 > 0) {
//                    // 处理回复数据
//                    //System.out.println("红灯亮回复数据：" +
//                            new String(readBuffer, 0, bytesRead1));
//                }
            } finally {
                closePort(serialPort);
            }
        } else {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
//            Map map = new HashMap();
//            map.put("message", "..串口连接失败，请检查串口("+
//                    jSerialCommIOProperties.getPortName()+")..");
//            String json = JSON.toJSONString(map);
//            webSocketServer.sendToAllClient(json);
            //System.out.println("红灯亮串口连接失败！");
            log.info("红灯亮串口连接失败！");
        }
    }

//    @Override
//    public void redOut() {
//        SerialPort serialPort = SerialPort.getCommPort(jSerialCommIOProperties.getPortName());
//        //System.out.println("连接串口："+jSerialCommIOProperties.getPortName());
//        boolean x = openPort(serialPort);
//        //System.out.println("串口状态："+x);
//        if (openPort(serialPort)) {
//            //System.out.println("串口连接成功！");
//            //System.out.println("红灯灭");
//            try {
//                //红灯灭00 5A 53 00 06 02 00 00 B5
//                byte[] buffer = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0xB5
//                };
//                serialPort.writeBytes(buffer, buffer.length);
//
//                // 读取回复数据
////                byte[] readBuffer = new byte[1024];
////                int bytesRead1 = serialPort.readBytes(readBuffer, readBuffer.length);
////                if (bytesRead1 > 0) {
////                    // 处理回复数据
////                    //System.out.println("红灯灭回复数据：" +
////                            new String(readBuffer, 0, bytesRead1));
////                }
//            } finally {
//                closePort(serialPort);
//            }
//        } else {
//            //System.out.println("串口连接失败！");
//        }
//    }

    @Override
//    @Async
    public void greenUp() {
        SerialPort serialPort = SerialPort.getCommPort(jSerialCommIOProperties.getPortName());
        //System.out.println("连接串口："+jSerialCommIOProperties.getPortName());
        boolean x = openPort(serialPort);
        //System.out.println("串口状态："+x);
        if (openPort(serialPort)) {
            //System.out.println("绿灯亮串口连接成功！");
            log.info("绿灯亮串口连接成功！");
            try {
                //红灯灭00 5A 53 00 06 02 00 00 B5
                byte[] buffer1 = new byte[] {
                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0xB5
                };
//                //黄灯灭00 5A 53 00 06 04 00 00 B7
//                byte[] buffer2 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0xB7
//                };
                //绿灯亮00 5A 53 00 05 08 00 00 BA
                byte[] buffer = new byte[] {
                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x05, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0xBA
                };
                serialPort.writeBytes(buffer1, buffer.length);
//                serialPort.writeBytes(buffer2, buffer.length);
                serialPort.writeBytes(buffer, buffer.length);
                
                // 读取回复数据
//                byte[] readBuffer = new byte[1024];
//                int bytesRead1 = serialPort.readBytes(readBuffer, readBuffer.length);
//                if (bytesRead1 > 0) {
//                    // 处理回复数据
//                    //System.out.println("绿灯亮回复数据：" +
//                            new String(readBuffer, 0, bytesRead1));
//                }
            } finally {
                closePort(serialPort);
            }
        } else {
//            Map map = new HashMap();
//            map.put("message", "..串口连接失败，请检查串口("+
//                    jSerialCommIOProperties.getPortName()+")..");
//            String json = JSON.toJSONString(map);
//            webSocketServer.sendToAllClient(json);
//            //System.out.println("绿灯亮串口连接失败！");
//            log.info("绿灯亮串口连接失败！");
        }
    }

//    @Override
//    public void greenOut() {
//        SerialPort serialPort = SerialPort.getCommPort(jSerialCommIOProperties.getPortName());
//        //System.out.println("连接串口："+jSerialCommIOProperties.getPortName());
//        boolean x = openPort(serialPort);
//        //System.out.println("串口状态："+x);
//        if (openPort(serialPort)) {
//            //System.out.println("串口连接成功！");
//            //System.out.println("绿灯灭");
//            try {
//                //绿灯灭00 5A 53 00 06 08 00 00 BB
//                byte[] buffer = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0xBB
//                };
//                serialPort.writeBytes(buffer, buffer.length);
//
//                // 读取回复数据
////                byte[] readBuffer = new byte[1024];
////                int bytesRead1 = serialPort.readBytes(readBuffer, readBuffer.length);
////                if (bytesRead1 > 0) {
////                    // 处理回复数据
////                    //System.out.println("绿灯灭回复数据：" +
////                            new String(readBuffer, 0, bytesRead1));
////                }
//            } finally {
//                closePort(serialPort);
//            }
//        } else {
//            //System.out.println("串口连接失败！");
//        }
//    }

//    @Override
////    @Async//黄灯刷的比较频繁，不建议分线程，会阻塞掉其他COM口
//    public void yellowUp() {
//        SerialPort serialPort = SerialPort.getCommPort(jSerialCommIOProperties.getPortName());
//        //System.out.println("连接串口："+jSerialCommIOProperties.getPortName());
//        boolean x = openPort(serialPort);
//        //System.out.println("串口状态："+x);
//        if (openPort(serialPort)) {
//            //System.out.println("黄灯亮串口连接成功！");
//            try {
//                //红灯灭00 5A 53 00 06 02 00 00 B5
//                byte[] buffer1 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0xB5
//                };
//                //绿灯灭00 5A 53 00 06 08 00 00 BB
//                byte[] buffer2 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0xBB
//                };
//                //黄灯亮00 5A 53 00 05 04 00 00 B6
//                byte[] buffer = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x05, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0xB6
//                };
//                serialPort.writeBytes(buffer1, buffer.length);
//                serialPort.writeBytes(buffer2, buffer.length);
//                serialPort.writeBytes(buffer, buffer.length);
//
//                // 读取回复数据
////                byte[] readBuffer = new byte[1024];
////                int bytesRead1 = serialPort.readBytes(readBuffer, readBuffer.length);
////                if (bytesRead1 > 0) {
////                    // 处理回复数据
////                    //System.out.println("黄灯亮回复数据：" +
////                            new String(readBuffer, 0, bytesRead1));
////                }
//            } finally {
//                closePort(serialPort);
//            }
//        } else {
////            Map map = new HashMap();
////            map.put("message", "..串口连接失败，请检查串口("+
////                    jSerialCommIOProperties.getPortName()+")..");
////            String json = JSON.toJSONString(map);
////            webSocketServer.sendToAllClient(json);
//            //System.out.println("黄灯亮串口连接失败！");
//        }
//    }

//    @Override
//    public void yellowOut() {
//        SerialPort serialPort = SerialPort.getCommPort(jSerialCommIOProperties.getPortName());
//        //System.out.println("连接串口："+jSerialCommIOProperties.getPortName());
//        boolean x = openPort(serialPort);
//        //System.out.println("串口状态："+x);
//        if (openPort(serialPort)) {
//            //System.out.println("串口连接成功！");
//            //System.out.println("黄灯灭");
//            try {
//                //黄灯灭00 5A 53 00 06 04 00 00 B7
//                byte[] buffer = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0xB7
//                };
//                serialPort.writeBytes(buffer, buffer.length);
//
//                // 读取回复数据
////                byte[] readBuffer = new byte[1024];
////                int bytesRead1 = serialPort.readBytes(readBuffer, readBuffer.length);
////                if (bytesRead1 > 0) {
////                    // 处理回复数据
////                    //System.out.println("黄灯灭回复数据：" +
////                            new String(readBuffer, 0, bytesRead1));
////                }
//            } finally {
//                closePort(serialPort);
//            }
//        } else {
//            //System.out.println("串口连接失败！");
//        }
//    }
}
